package vleon.app;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class SceneStory {
	String startDate;
	String imgName, imgUrl, storyName, storySearchUrl, storyTimestamp;
	String copyright;
	ArrayList<HashMap<String, String>> storyHotspots;
	
	public SceneStory() {
		startDate = null;
		imgName = null;
		imgUrl = null;
		storyName = null;
		storySearchUrl = null;
		storyTimestamp = null;
		copyright = null;
		storyHotspots = new ArrayList<HashMap<String, String>>();
	}

	public void save(MySqliteOpenHelper sqliteHelper) {
		SQLiteDatabase dbWrite = sqliteHelper.getWritableDatabase();
		String hotstring = "";
		for (HashMap<String, String> hashMap : storyHotspots) {
			String hotspot = hashMap.get("hotspot") + "##"
					+ hashMap.get("link");
			hotstring += hotspot + "&&&";
		}
		hotstring = hotstring.substring(0, hotstring.length() - 3);
//		ContentValues values = new ContentValues();
//		values.put("storyname", storyName);
//		values.put("imgname", imgName);
//		values.put("storyhots", hotstring);
//		values.put("copyright", copyright);
//		values.put("imgurl", imgUrl);
//		dbWrite.insert("bingscene", null, values);
		dbWrite.execSQL("insert into bingscene(storyname,imgname,storyhots,copyright,imgurl,startdate) values(?,?,?,?,?,?)", 
				new Object[]{storyName,imgName,hotstring,copyright,imgUrl,startDate});
		dbWrite.close();
	}
	public static SceneStory readFromdb(MySqliteOpenHelper sqliteHelper, int _id){
		SQLiteDatabase dbRead = sqliteHelper.getReadableDatabase();
		Cursor cursor = dbRead.rawQuery("select * from bingscene where _id=?", new String[]{_id+""});
		if (cursor != null && cursor.getCount() == 1){
			cursor.moveToFirst();
			SceneStory scene = new SceneStory();
			scene.storyName = cursor.getString(1);
			scene.copyright = cursor.getString(2);
			scene.imgName = cursor.getString(3);
			scene.imgUrl = cursor.getString(5);
			scene.storyTimestamp = cursor.getString(6);
			scene.startDate = cursor.getString(7);
			String hotstring = cursor.getString(4);
			if(hotstring!=null){
				String[] hotspots = hotstring.split("&&&");
				for (String hotspot : hotspots) {
					String[] temp = hotspot.split("##");
					HashMap<String, String> hashMap = new HashMap<String, String>();
					hashMap.put("hotspot", temp[0]);
					hashMap.put("link", temp[1]);
					scene.storyHotspots.add(hashMap);
				}
			}
			return scene;
		}
		return null;
	}
}
